package com.ciwong.epaper.modules.reciteWords.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.common.guidedialog.BaseGuideLayerDialog;
import com.ciwong.epaper.modules.reciteWords.bean.JiaoCaiDetailBean;
import com.ciwong.epaper.modules.reciteWords.widget.BookItemView;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.libs.utils.CWSys;
import com.ciwong.mobilelib.application.BaseApplication;

import java.util.List;

/**
 * Created by keepon on 2018/8/7.
 */
public class BookAdapter extends BaseRecyclerViewAdapter<JiaoCaiDetailBean, BookAdapter.ViewHolder> {


	public BookAdapter(Context context, List<JiaoCaiDetailBean> datas) {
		super(context, datas);
	}

	@Override
	public ViewHolder onCreateMyViewHolder(@NonNull ViewGroup parent, int viewType) {
		ViewHolder newBookDeskViewHolder = new ViewHolder(new BookItemView(mContext));
		return newBookDeskViewHolder;
	}

	@Override
	public void onBindMyViewHolder(@NonNull ViewHolder holder, int position) {
		holder.bindData(position, mDatas.get(position));
	}


	public class ViewHolder extends RecyclerView.ViewHolder {

		public ViewHolder(View itemView) {
			super(itemView);
		}

		public void bindData(final int position, final JiaoCaiDetailBean jiaoCaiDetailBean) {
			if (itemView instanceof BookItemView) {
				BookItemView bookItemView = (BookItemView) itemView;
				bookItemView.bindData(jiaoCaiDetailBean);
				if (position == 0) {
					setGuideByPlatform(jiaoCaiDetailBean, bookItemView);
				}
		}
			itemView.setOnClickListener(new View.OnClickListener()

		{
			@Override public void onClick (View v){
			if (mOnItemClickListener != null) {
				mOnItemClickListener.onItemClick(position, jiaoCaiDetailBean);

			}
		}
		});
	}

}

	private void setGuideByPlatform(final JiaoCaiDetailBean jiaoCaiDetailBean, BookItemView bookItemView) {
		try {
			boolean isShowHelp = CWSys.getSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_SELECT_WORD_BOOK + BaseApplication.getInstance().getUserInfoBase().getUserId(), true);
			if (isShowHelp) {
				if (EApplication.PLATFORM_ENV == EApplication.PLATFORM_PAD) {
					final BookItemView bookItemViewFinal = bookItemView;
					bookItemViewFinal.postDelayed(new Runnable() {
						@Override
						public void run() {
							initGuide(jiaoCaiDetailBean, bookItemViewFinal.mRlContainer.getLeft());
						}
					}, 400);
				}else{
					initGuide(jiaoCaiDetailBean, 0);
				}
			}
		}catch (Exception e){

		}

	}

	BaseGuideLayerDialog dialog;

	private void initGuide(JiaoCaiDetailBean jiaoCaiDetailBean, int left) {
		try {
			if (dialog != null && dialog.isShowing()) {
				return;
			}
			if (dialog == null)
				dialog = new BaseGuideLayerDialog(mContext, R.style.DialogStyleGuide);
			View view;
			view = LayoutInflater.from(mContext).inflate(R.layout.dialog_select_word_book_tips, null);


			RelativeLayout relativeLayout = view.findViewById(R.id.rl_container);
			RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
			if (layoutParams != null) {
				if (EApplication.PLATFORM_ENV == EApplication.PLATFORM_PAD && left > 0) {
					layoutParams.leftMargin = (int) (mContext.getResources().getDimension(R.dimen.dp_5) + left);
				}
				if (android.os.Build.MODEL.equalsIgnoreCase("MI 8")) {
					if (layoutParams != null) {
						layoutParams.topMargin = layoutParams.topMargin - mContext.getResources().getDimensionPixelSize(R.dimen.dp_26);
					}
				}
			}
			if (jiaoCaiDetailBean != null) {
				TextView tvBookVersion = view.findViewById(R.id.tv_book_version);
				TextView tvGradeName = view.findViewById(R.id.tv_grade_name);
				TextView tvWordCount = view.findViewById(R.id.tv_word_count);
				TextView tvLearnCount = view.findViewById(R.id.tv_learn_count);
				tvBookVersion.setText(jiaoCaiDetailBean.getVersionFullName());
				String gradeName = jiaoCaiDetailBean.getGradeName();
				if (jiaoCaiDetailBean.getGradeName().endsWith("学期")) {
					gradeName = gradeName.substring(0, gradeName.indexOf("学期"));
				}
				if (jiaoCaiDetailBean.getStatus() > 0 && jiaoCaiDetailBean.getLearnCount() > 0) {
					tvLearnCount.setVisibility(View.VISIBLE);
					tvLearnCount.setText("已学" + jiaoCaiDetailBean.getLearnCount() + "遍");
				} else {
					tvLearnCount.setVisibility(View.GONE);
				}
				tvGradeName.setText(gradeName);
				tvWordCount.setText(jiaoCaiDetailBean.getWordsCount() + "个单词");

			}
			dialog.setContentView(view);
			dialog.setCancelable(false);
			dialog.findViewById(R.id.tx_perusal_know).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					CWSys.setSharedBoolean(EConstants.SHARE_KEY_IS_SHOW_SELECT_WORD_BOOK + BaseApplication.getInstance().getUserInfoBase().getUserId(), false);
					dialog.dismiss();
					dialog = null;

				}
			});
			dialog.show();

		} catch (Exception e) {

		}

	}


}
